/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */
package com.ykht.proj.activity.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.csp.sentinel.util.StringUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codingapi.tx.annotation.TxTransaction;
import com.ykht.proj.activity.entity.VEnt;
import com.ykht.proj.activity.mapper.VEntMapper;
import com.ykht.proj.activity.service.VEntService;
import com.ykht.proj.activity.vo.VEntVo;
import com.ykht.proj.admin.api.Enum.UserRoleTypeEnum;
import com.ykht.proj.admin.api.dto.UserDTO;
import com.ykht.proj.admin.api.entity.SysUser;
import com.ykht.proj.admin.api.feign.RemoteUserService;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.common.security.util.SecurityUtils;
import com.ykht.proj.coupon.Enum.UserTypeEnum;
import com.ykht.proj.coupon.entity.CnShop;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * 企业（客户）表
 *
 * @author csg
 * @date 2021-05-21 14:22:59
 */
@RequiredArgsConstructor
@Slf4j
@Service
public class VEntServiceImpl extends ServiceImpl<VEntMapper, VEnt> implements VEntService {

	private final VEntMapper vEntMapper;
	private final RemoteUserService remoteUserService;

	/**
	 * 新增企业（客户）表 并添加 用户账号
	 *
	 * @param vEntVo
	 * @return
	 */
	@TxTransaction(isStart = true) // 请求方 要加 isStart = true  被请求的不用加  isStart = true  控制事物 注解
	@Transactional(rollbackFor = Exception.class)
	@Override
	public R saveAndUser(VEntVo vEntVo) {
		vEntVo.setAbbreviation(StringUtil.isEmpty(vEntVo.getAbbreviation()) ? vEntVo.getName() : vEntVo.getAbbreviation());
		vEntVo.setCreateTime(LocalDateTime.now());
		vEntVo.setBindTime(LocalDateTime.now());
		vEntVo.setCreateUser(SecurityUtils.getUserId());
		vEntVo.setUserType(UserTypeEnum.customer_company.getValue());
		VEnt vEnt = BeanUtil.toBean(vEntVo, VEnt.class);
		this.save(vEnt);
		vEntVo.setId(vEnt.getId());
		if (vEnt.getLoginId() != null) {
			SysUser user = new SysUser();
			user.setUserId(Integer.parseInt(vEnt.getLoginId().toString()));
			user.setSjIdDefLogin(vEnt.getId());
			R bindSj = remoteUserService.bindSj(user);
		}
		if (vEntVo.getLoginId() == null) {
			UserDTO userDTO = new UserDTO();
			userDTO.setUsername(StringUtils.isEmpty(vEntVo.getUsername()) ? vEntVo.getName() : vEntVo.getUsername());
			userDTO.setUserType(UserTypeEnum.customer_company.getValue());
			userDTO.setPassword("123456");
			userDTO.setParentId(vEntVo.getParentId());
			userDTO.setRole(UserRoleTypeEnum.customer_company.getValue());
			userDTO.setSjIdDefLogin(vEnt.getId());
			R<UserDTO> userDTOR = remoteUserService.saveUser(userDTO);
			if (userDTOR.getCode() != 0) {
				throw new RuntimeException(userDTOR.getMsg());
			}
			vEnt.setLoginId(Long.valueOf(userDTOR.getData().getUserId()));
			this.updateById(vEnt);
		}
		return R.ok(vEntVo);
	}

	/**
	 * 企业列表 分页查询
	 *
	 * @param page   分页对象
	 * @param vEntVo 企业（客户）表
	 * @return
	 */
	@Override
	public IPage<VEntVo> getVEntPage(Page page, VEntVo vEntVo) {
		return vEntMapper.getVEntPage(page, vEntVo);
	}
}
